package com.kkb.billmngapi.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.billmngapi.service.BillService;
import com.kkb.billmngapi.service.BillTypeService;
import com.kkb.springcloudbill.entity.Bill;
import com.kkb.springcloudbill.entity.BillType;
import com.kkb.springcloudbill.entity.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private BillTypeService billTypeService;

    private Bill bill = new Bill();

    private Integer pageNum;

    private Integer pageSize;

//    @RequestMapping("/test")
//    @ResponseBody
//    public String test(){
//        return "test";
//    }

    @RequestMapping("/")
    public ResponseEntity<PageInfo<Bill>> initPage(@RequestParam(required = false) Bill bill,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "10") Integer pageSize){
        if (bill != null){
            bill = new Bill();
        }
        this.bill = bill;

        PageInfo<Bill> pageInfo = billService.loadAllBill(bill, pageNum, pageSize);
        this.pageNum = pageNum;
        this.pageSize = pageSize;

        List<BillType> billTypes = billTypeService.getAllBillType();
        return ResponseEntity.ok(pageInfo);
    }

    @RequestMapping(value = "/billtest")
    public ResponseEntity<ResultVo> billtest(@RequestBody(required = false) Bill bill,
                                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        if (bill == null){
            bill = new Bill();
        }
        this.bill = bill;
        PageInfo<Bill> pageInfo = billService.loadAllBill(bill, pageNum, pageSize);
        this.pageNum = pageNum;
        this.pageSize = pageSize;

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("pageInfo", pageInfo);

        List<BillType> billTypes = billTypeService.getAllBillType();
        hashMap.put("billTypes", billTypes);

        ResultVo succeed = ResultVo.SUCCEED();
        succeed.setData(hashMap);
        return ResponseEntity.ok(succeed);
    }

    @RequestMapping(value = "/billtest-nonbill")
    public ResponseEntity<ResultVo> billsNonBill(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<Bill> pageInfo = billService.loadAllBill(null, pageNum, pageSize);
        this.pageNum = pageNum;
        this.pageSize = pageSize;

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("pageInfo", pageInfo);

        List<BillType> billTypes = billTypeService.getAllBillType();
        hashMap.put("billTypes", billTypes);

        ResultVo succeed = ResultVo.SUCCEED();
        succeed.setData(hashMap);
        return ResponseEntity.ok(succeed);
    }

    @RequestMapping("/billtype")
    public ResponseEntity<List<BillType>> getAllType(){
        List<BillType> billTypes = billTypeService.getAllBillType();
        return ResponseEntity.ok(billTypes);
    }

    @RequestMapping("/bill/update/{billId}")
    ResponseEntity<Bill> getBillById(@PathVariable Integer billId){
        Bill billById = billService.getBillById(billId);
        return ResponseEntity.ok(billById);
    }

    @RequestMapping(value = "/bill/update")
    public ResponseEntity<Integer> updateBillById(@RequestBody Bill bill){
        System.out.println("===================update" + bill + "====================");
        int i = billService.updateBillById(bill);
        return ResponseEntity.ok(i);
    }

    //删除账单
    @RequestMapping(value = "/bill/delete/{billId}")
    public ResponseEntity<Integer> deleteBill(
            @PathVariable("billId") Integer billId,
            Model model){
        int i = billService.deleteBillById(billId);
        return ResponseEntity.ok(i);
    }

    //执行insert新的bill信息
    @RequestMapping(value = "/bill/add")
    public ResponseEntity<Integer> addBill(@RequestBody Bill bill, Model model){
        System.out.println("add-------------------------------" + bill);
        int i = billService.addNewBill(bill);
        return ResponseEntity.ok(i);
    }
}
